﻿Imports BUS
Imports DTO

Public Class frmQuanLySachTrongKe

    Public us_control As UserChiTietSach

    Private Sub frmQuanLySachTrongKe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbKeSach.DataSource = KeSach_BUS.loadKeSach
        cbbKeSach.DisplayMember = "TenKe"
        cbbKeSach.ValueMember = "MaKe"
    End Sub

    Private Sub cbbKeSach_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbKeSach.SelectedIndexChanged

        GroupBox1.Controls.Clear()

        Dim ks As KeSach_DTO = CType(cbbKeSach.SelectedItem, KeSach_DTO)
        lblSoNgan.Text = ks.soNgan
        lblSoTang.Text = ks.soTang

        'Dim dt As DataTable = ViTri_BUS.getViTriTheoMaKe(ks.maKe)
        Try
            Dim dtsach As DataTable = CuonSach_BUS.getSachTheoMaKe(ks.maKe)
            REM: tạo user control
            'Tạo tọa độ
            Dim x As Integer = 100
            Dim y As Integer = 90
            Dim vitri As Integer = 0

            For i As Integer = 0 To ks.soTang - 1
                For j As Integer = 0 To ks.soNgan - 1
                    Try
                        us_control = New UserChiTietSach
                        us_control.Width = 65
                        us_control.Height = 90
                        us_control.Top = 20 + (x * i)
                        us_control.Left = 10 + (y * j)
                        us_control.lblTinhTrang.Text = dtsach.Rows(vitri)("TenTinhTrang").ToString
                        us_control.btnSach.BackgroundImage = _
                                            Image.FromFile(dtsach.Rows(vitri)(0).ToString)
                        us_control.btnSach.Tag = dtsach.Rows(vitri)("MaSach")
                        Me.GroupBox1.Controls.Add(us_control)
                    Catch ex As Exception
                        Continue For
                    End Try
                    
                    vitri = vitri + 1
                    AddHandler us_control.btnSach.Click, AddressOf myButtonClick
                Next
            Next
        Catch ex As Exception
            MessageBox.Show("Không có sách trong kệ này")
        End Try

    End Sub

    Private Sub myButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim bt As Button = CType(sender, Button)
        Dim f As New frmSach(bt)
        f.ShowDialog()
    End Sub

End Class